﻿
// ================================================================================
//        File: frmEmployeeManager.cs.cs
//        Desc: 显示所有员工
//        Auth: JF
//        Date: 2010-09-22
// ================================================================================
//        Change History
// ================================================================================
//        Date:         Author:                  Description:
//        --------     --------              -------------------
//              
// ================================================================================
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using EShop.BLL;
using EShop.Model;
using EShop.Common;

namespace EShop.Client
{
    public partial class frmEmployeeManager : frmBase
    {
        public frmEmployeeManager()
        {
            InitializeComponent();
            tlscbxClass.SelectedIndex = 0;
        }

        private int iTotal = 0;//总的记录条数
        private int iPageCount = 0;//总的页数
        private int iCurrentPage = 0;//当前页号
        private int iCurrentRow = 0;//当前记录行

        DataSet ds = new DataSet();
        DataTable dt;
        private DataTable dtTemp = new DataTable();

        bool boolSearch = false;


        private void frmEmployeeManager_Load(object sender, EventArgs e)
        {
            bindingNavigatorAddNewItem.Enabled = true;
            bindingNavigatorMoveFirstItem.Enabled = true;//首页
            bindingNavigatorMovePreviousItem.Enabled = true;//上一页
            bindingNavigatorPositionItem.Enabled = true;//转入页
            bindingNavigatorMoveNextItem.Enabled = true;//下一页
            bindingNavigatorMoveLastItem.Enabled = true;//最后一页
            GetAllEmployeeInfo();
        }
        private void GetAllEmployeeInfo()
        {
            try
            {
                EmployeeBLL oEmployeeBLL = new EmployeeBLL();
                if (boolSearch)
                {
                    iCurrentPage = 0;
                    iCurrentRow = 0;

                }
                else
                {
                    dt = oEmployeeBLL.QueryEmployeeInfo(null);
                }
                iTotal = dt.Rows.Count;
                if (iTotal > 0)
                {
                    InitDataSet();
                }
            }
            catch
            { }
        }

        private void InitDataSet()
        {
            iPageCount = (iTotal / oShopConfigInfo.PageSize);
            if (iTotal % oShopConfigInfo.PageSize != 0) iPageCount = iPageCount + 1;//获取总页数
            bindingNavigatorCountItem.Text = "/" + iPageCount.ToString();
            bindingNavigatorCountItem.Enabled = true;
            LoadData();
        }

        private void LoadData()
        {
            int nStartPos = 0;//当前页面的开始记录行
            int nEndPos = 0;//当前页面的结束记录行
            DataTable dtTemp = dt.Clone();
            nStartPos = iCurrentRow;
            if ((iCurrentPage * oShopConfigInfo.PageSize + oShopConfigInfo.PageSize) <= iTotal)
            {
                nEndPos = iCurrentRow + oShopConfigInfo.PageSize;
            }
            else
            {
                nEndPos = iCurrentRow + (iTotal % oShopConfigInfo.PageSize);
            }
            nStartPos = iCurrentRow;
            bindingNavigatorCountItem.Text = string.Format(bindingNavigatorCountItem.Text, iPageCount.ToString());
            bindingNavigatorPositionItem.Text = (iCurrentPage + 1).ToString();
            for (int i = nStartPos; i < nEndPos; i++)
            {
                dtTemp.ImportRow(dt.Rows[i]);
                //iCurrentPage++;
            };
            //int iIndex = -1;
            int iCount = dtTemp.Rows.Count;
            if (iCount <= 0) return;
            else
            {
                dgvEmployeeManager.Rows.Clear();
                for (int i = 0; i < iCount; i++)
                {
                    //iIndex = dgvEmployeeManager.Rows.Add();
                    DataGridViewRow dr = new DataGridViewRow();
                    dr.CreateCells(dgvEmployeeManager);

                    int ii = dr.Cells.Count;
                        //dgvEmployeeManager.Rows[iIndex].Cells["EmployeeID"].Value = dtTemp.Rows[i]["EmployeeID"].ToString();
                        //dgvEmployeeManager.Rows[iIndex].Cells["FullName"].Value = dtTemp.Rows[i]["FullName"].ToString();
                        //dgvEmployeeManager.Rows[iIndex].Cells["Sex"].Value = ((bool)dtTemp.Rows[i]["Sex"]) == false ? "男" : "女";
                        //dgvEmployeeManager.Rows[iIndex].Cells["Brithday"].Value = Convert.ToDateTime(dtTemp.Rows[i]["Brithday"]).ToString("yyyy-MM-dd");
                        //dgvEmployeeManager.Rows[iIndex].Cells["IsGregorianCalendar"].Value = ((bool)dtTemp.Rows[i]["IsGregorianCalendar"]) == false ? "农历" : "公历";
                        //dgvEmployeeManager.Rows[iIndex].Cells["LastLoginDatetime"].Value = dtTemp.Rows[i]["LastLoginDatetime"].ToString();

                        dr.Cells[0].Value = dtTemp.Rows[i]["EmployeeID"].ToString();
                        dr.Cells[1].Value = dtTemp.Rows[i]["FullName"].ToString();
                        dr.Cells[2].Value = ((bool)dtTemp.Rows[i]["Sex"]) == false ? "男" : "女";
                        dr.Cells[3].Value = Convert.ToDateTime(dtTemp.Rows[i]["Brithday"]).ToString("yyyy-MM-dd");
                        dr.Cells[4].Value = ((bool)dtTemp.Rows[i]["IsGregorianCalendar"]) == false ? "农历" : "公历";
                        dr.Cells[5].Value = dtTemp.Rows[i]["LastLoginDatetime"].ToString();
                        dgvEmployeeManager.Rows.Add(dr);
                }
            }
        }
        

        //首页
        private void bindingNavigatorMoveFirstItem_Click(object sender, EventArgs e)
        {
            iCurrentPage = 0;
            iCurrentRow = oShopConfigInfo.PageSize * iCurrentPage;
            LoadData();
        }
       
        //上一页
        private void bindingNavigatorMovePreviousItem_Click(object sender, EventArgs e)
        {
            if (iCurrentPage == 0) return;
            iCurrentPage = iCurrentPage - 1;
            iCurrentRow = oShopConfigInfo.PageSize * iCurrentPage;
            LoadData();
        }
       
        //下一页
        private void bindingNavigatorMoveNextItem_Click(object sender, EventArgs e)
        {
            if ((iCurrentPage+1) == iPageCount) return;
            iCurrentPage = iCurrentPage + 1;
            iCurrentRow = oShopConfigInfo.PageSize * iCurrentPage;
            LoadData();
        }
        //最后一页
        private void bindingNavigatorMoveLastItem_Click(object sender, EventArgs e)
        {
            if (iCurrentPage == iPageCount - 1) return;
            iCurrentPage = iPageCount - 1;
            iCurrentRow = oShopConfigInfo.PageSize * iCurrentPage - 1;
            LoadData();
        }

        //跳转输入窗口
        private void bindingNavigatorPositionItem_TextChanged(object sender, EventArgs e)
        {
            try
            {
                int i = Convert.ToInt32(bindingNavigatorPositionItem.Text.Trim());
                if(i>iPageCount)
                    bindingNavigatorPositionItem.Text = (iCurrentPage + 1).ToString();
            }
            catch
            {
                bindingNavigatorPositionItem.Text = (iCurrentPage+1).ToString();
            }
        }
        
        //跳转
        private void bindingNavigatorPositionItem_KeyPress(object sender, KeyPressEventArgs e)
        {
            int num=Convert.ToInt32(bindingNavigatorPositionItem.Text.Trim());
            if (e.KeyChar == (char)13 && num != iCurrentPage && num <= iPageCount & num > 0)
            {
                iCurrentPage = (num-1);
                iCurrentRow = oShopConfigInfo.PageSize * iCurrentPage;
                LoadData();
            }
            else
            {
                bindingNavigatorPositionItem.Text = (iCurrentPage+1).ToString();
            }
        }

        //添加员工
        private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
        {
            frmEmployeeReg oEmployeeReg = new frmEmployeeReg();
            oEmployeeReg.Owner = this;
            if (oEmployeeReg.ShowDialog() == DialogResult.OK)
            {
                GetAllEmployeeInfo();
            }
        }

        //设置员工权限
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            string strEmployeeID = string.Empty;
            try
            {
                int index = dgvEmployeeManager.SelectedRows[0].Index;
                strEmployeeID = dgvEmployeeManager.Rows[index].Cells[0].Value.ToString();
                frmPermissionManger oPermissionManager = new frmPermissionManger(strEmployeeID);
                oPermissionManager.Owner = this;
                oPermissionManager.ShowDialog();
            }
            catch { }
            //
        }

        //添加员工
        private void 添加员工ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            bindingNavigatorAddNewItem_Click(sender, e);
        }
        
        //查看员工
        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            string strEmployeeID = string.Empty;
            try
            {
                strEmployeeID = this.dgvEmployeeManager.SelectedRows[0].Cells[0].Value.ToString();
                if (string.IsNullOrEmpty(strEmployeeID))
                { }
                else
                {
                    frmEmployeeInfoEdit oEmployeeInfoEdit = new frmEmployeeInfoEdit(strEmployeeID);
                    if (oEmployeeInfoEdit.ShowDialog() == DialogResult.OK)
                    {                        GetAllEmployeeInfo();
                    }
                }
            }
            catch { }
        }

        private void 权限ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            toolStripButton1_Click(sender, e);
        }

        private void 查看详情ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            toolStripButton2_Click(sender, e);
        }

        private void dgvEmployeeManager_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            toolStripButton2_Click(sender, new EventArgs());
        }

        private void tslbtnSearch_Click(object sender, EventArgs e)
        {
            //开始查询
            string keyWord = SystemLibrary.SQLQuoteName(tsslKeyword.Text.Trim());
            if (string.IsNullOrEmpty(tsslKeyword.Text)) return;
            dgvEmployeeManager.Rows.Clear();
            boolSearch = true;
            dt = new EmployeeBLL().SearchEmployee(tsslKeyword.Text.Trim(), tlscbxClass.SelectedIndex);
            GetAllEmployeeInfo();
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            //清楚结果
            if (boolSearch == false) return;
            boolSearch = false;
            GetAllEmployeeInfo();
        }

        private void frmEmployeeManager_FormClosing(object sender, FormClosingEventArgs e)
        {
            dgvEmployeeManager.Dispose();
        }

        //private void DataGridViewEndureValue()
        //{
        //    AsyncMethod.ControlsEndueValueEventHandler OnDataGridViewEndureValue = new AsyncMethod.ControlsEndueValueEventHandler(OnDataGridValueEndueValueEvent);
        //    this.BeginInvoke(OnDataGridViewEndureValue);
        //}
    }
}
